<template>
  <div class="nickname-container mtop">
    <m-header title="昵称"></m-header>
    <div class="input-wrapper">
      <div class="label">昵称：</div>
      <div class="comp">
        <input v-model="nickName" placeholder="请输入昵称">
      </div>
    </div>
    <div class="button-wrapper">
      <div class="btn" @click="handleSubmit">确定</div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';

export default {
  name: 'NickName',
  data () {
    return {
      nickName: ''
    };
  },
  computed: {
    ...mapGetters({
      userInfo: 'mine/userInfo'
    })
  },
  mounted () {
    if (this.userInfo.nickname) {
      this.nickName = this.userInfo.nickname;
    } else {
      this.getUseInfo();
    }
  },
  methods: {
    ...mapActions({
      getMineInfo: 'mine/mineInfo',
      setMemberInfo: 'common/setMemberInfo'
    }),
    handleSubmit () {
      const params = {
        // avatar: this.userInfo.avatar,
        nickname: this.nickName
      }
      this.setMemberInfo(params).then(() => {
        this.$vux.toast.text('修改昵称成功');
        setTimeout(() => {
          this.$router.replace({ path: '/setting' });
        }, 1500);
      })
    },
    getUseInfo () {
      this.getMineInfo().then(res => {
        this.nickName = res.nickname;
      });
    }
  }
};
</script>
<style lang="less" scoped>
.nickname-container  {
  .input-wrapper {
    .label {
      font-size: 0.28rem;
      color: #666666;
      padding: 0.3rem 0 0.2rem 0.2rem;
    }
    .comp {
      height: 0.8rem;
      background: #ffffff;
      position: relative;
      input {
        width: 100%;
        padding: 0.1rem 0.6rem 0.1rem 0.3rem;
        height: 0.8rem;
        line-height: 0.8rem;
        color: #999999;
        box-sizing: border-box;
        &::-webkit-input-placeholder {
          // font-size: 0.28rem;
          color: #999999;
        }
      }
    }
  }
  .button-wrapper {
    margin-top: 0.6rem;
    padding: 0 0.2rem;
    .btn {
      text-align: center;
      height: 0.88rem;
      line-height: 0.88rem;
      font-size: 0.34rem;
      color: #ffffff;
      background: #d44743;
      cursor: pointer;
      border-radius: 0.1rem;
    }
  }
}
</style>
